package b4.view.component.smithy
{
	import b4.model.vo.player.BasicObj;
	import b4.model.vo.player.Equip;
	import b4.model.vo.player.Obj;
	import b4.model.vo.player.Pack;
	import b4.view.component.events.ItemClickEvent;
	import b4.view.component.events.MsgEvent;
	import b4.view.component.pack.BasicObjItem;
	import b4.view.component.pack.PackageDialog;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import org.aswing.JFrame;
	import org.aswing.JTabbedPane;
	import org.aswing.border.*;
	
	public class Smithy extends JFrame
	{
		public var equipStren :EquipStren;
		public var equipBreak :EquipBreak;
		public var equipRefine :EquipRefine;
		public var equipTransfor :EquipTransfor;
		public var equipMerge :EquipMerge;
		public var equipGodBless :EquipGodBless;
		public var equipLevelUp :EquipLevelUp;
		public var itemMerge :ItemMerge;
		public var soulRefine :SoulRefine;
		public var leftTabPane:JTabbedPane;
		
		public function Smithy()
		{
			super();
			this.setIcon(null);
			this.setTitle("铁匠铺");
			this.setSizeWH(650, 600);
			this.setPreferredWidth(500);
			this.setLocationXY(280,0);
			this.setResizable(false);
			this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
			equipStren = new EquipStren();
			equipBreak = new EquipBreak();
			equipRefine = new EquipRefine();
			equipTransfor = new EquipTransfor();
			equipMerge = new EquipMerge();
			equipGodBless = new EquipGodBless();
			equipLevelUp = new EquipLevelUp();
			itemMerge = new ItemMerge();
			soulRefine = new SoulRefine();
//			var titleBar:JFrameTitleBar = new JFrameTitleBar();
//			frame.setTitleBar(titleBar)
//			frame.setComBoundsXYWH(10, 10, 600, 290);

			leftTabPane = new JTabbedPane();
			leftTabPane.setTabPlacement(JTabbedPane.LEFT);
			
			addTabPane(leftTabPane,equipStren,"装备强化");
			equipStren.addEventListener(ItemClickEvent.TAB_LOAD_ITEM_OFF, onLoadItemOff);	
			equipStren.strengthenBtn.addEventListener(MouseEvent.CLICK, beginBlackSmithyAction);
			
            addTabPane(leftTabPane,equipBreak, "装备分解");
            equipBreak.addEventListener(ItemClickEvent.TAB_LOAD_ITEM_OFF, onLoadItemOff);
            equipBreak.breakButton.addEventListener(MouseEvent.CLICK, beginBlackSmithyAction);
            
            addTabPane(leftTabPane,equipRefine, "装备洗练");
            equipRefine.addEventListener(ItemClickEvent.TAB_LOAD_ITEM_OFF, onLoadItemOff);
            equipRefine.refineButton.addEventListener(MouseEvent.CLICK, beginBlackSmithyAction);
            addTabPane(leftTabPane,equipTransfor,"装备转移");
            addTabPane(leftTabPane,equipMerge,"装备融合");
            addTabPane(leftTabPane,equipGodBless,"装备神佑");
            addTabPane(leftTabPane,equipLevelUp,"装备升级");
            addTabPane(leftTabPane,itemMerge,"物品合成");
            addTabPane(leftTabPane,soulRefine,"武魂淬炼");
			leftTabPane.addStateListener(loadOffAll);
			this.setContentPane(leftTabPane);
		}
		
		public function loadItem(obj:BasicObj):BoxObj
		{
			var selPane:BaseTab = leftTabPane.getSelectedComponent() as BaseTab;
			var temBoxObj:BoxObj;
			
			var rc:int = selPane.tryToLoadItem(obj);
			if (rc == 0)
			{
		    	if (!(obj is Equip))
		    	{
					var object:Obj = new Obj();
		    		object.getObjCopy(obj);
		    		object.num = 1;
					obj = object;
		    	}
				temBoxObj = selPane.loadItem(obj) as BoxObj;
				if (temBoxObj != null)
				{
					temBoxObj.getAsset().addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
					temBoxObj.getAsset().addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
				}	
			}
			else
			{
				dispatchMsgTipEvent(selPane.tryToLoadRs[rc])
			}
			return temBoxObj as BoxObj;
		}
		
		public function getLoadedItems():Array
		{
			var selPane:BaseTab = leftTabPane.getSelectedComponent() as BaseTab;
			return selPane.getLoadedItems();
		}
		
		public function loadOffAll(e:Event):void
		{
			equipStren.loadOffAll();
			equipBreak.loadOffAll();
			equipRefine.loadOffAll();
			equipTransfor.loadOffAll();
			equipMerge.loadOffAll();
			equipGodBless.loadOffAll();
			equipGodBless.loadOffAll();
			equipLevelUp.loadOffAll();
			itemMerge.loadOffAll();
			soulRefine.loadOffAll();
			dispatchLoadOffItemEvent();
		}
		
	    private function beginBlackSmithyAction(e:MouseEvent):void
		{
			var functionTab:BaseTab = e.target.parent.parent.parent as BaseTab;
			var data: Object = functionTab.getSendData();
			if (functionTab is EquipStren)
			{
				if (!equipStren.equipBox.hasObj)
				{
					dispatchMsgTipEvent("请选择合适的装备");
				}
				else if ( !equipStren.stenStoneBox.hasObj)
				{
					dispatchMsgTipEvent("请放入强化石");
				}
				else
				{
					data["method"] = "strengthenEquip";
					startDispatchEvent(data);
				}
			}
			else if (functionTab is EquipBreak)
			{
				if (!(equipBreak.equipBox_1.hasObj || equipBreak.equipBox_2.hasObj || equipBreak.equipBox_3.hasObj
							|| equipBreak.equipBox_4.hasObj))
				{
					dispatchMsgTipEvent("请先放入要分解的装备！");
				}
				
				data["method"] = "breakEquip";
				startDispatchEvent(data);
			}
			else if (functionTab is EquipRefine)
			{
				if(!equipRefine.equipBox.hasObj)
				{
					dispatchMsgTipEvent("请先放入要洗练的装备！");
				}
				else if(!equipRefine.stockBox.hasObj)
				{
					dispatchMsgTipEvent("请先放入洗练石！");
				}
				else
				{
					data["method"] = "refineEquip";
					startDispatchEvent(data);		
				}
			}
		}
		
		private function dispatchLoadOffItemEvent():void
		{
			var loadOffEvent:ItemClickEvent = new ItemClickEvent(ItemClickEvent.LOAD_SMITHY_ITEM_OFF);
			dispatchEvent(loadOffEvent);
		}
		
		private function dispatchMsgTipEvent(str:String):void
		{
			var msgData:Object = {"msg":str}
			var msgEvent:MsgEvent = new MsgEvent(MsgEvent.SMITHY_TIP_MSG);
			msgEvent.data = msgData;
			dispatchEvent(msgEvent);
		}
		
		public function showStrengthenResult(pack:Pack):void
		{	
			var equipList:Object = pack.smithyEquipList
			equipStren.loadOffAll();
			for each(var tempEquip:Equip in equipList)
			{
				var temBox:BoxObj = equipStren.loadItem(tempEquip) as BoxObj;
				if (temBox != null)
				{
					temBox.getAsset().addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
					temBox.getAsset().addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
//					temBox.addEventListener(ItemClickEvent.LOAD_ITEM_OFF, onLoadItemOff);	
				}
			}
		}
		
		public function showBreakResult(pack:Pack):void
		{
			var smithyObjList:Object = pack.smithyObjList;
			equipBreak.loadOffAll();
			for each(var tempObj:Obj in smithyObjList)
			{
				var temBox:BoxObj = equipBreak.loadItem(tempObj) as BoxObj;
				if (temBox != null)
				{
					temBox.getAsset().addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
					temBox.getAsset().addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
					var objItem:BasicObjItem = temBox.getAsset() as BasicObjItem
					objItem.setNumTxtState();
					
				}
			}
		}
		
		public function showRefineResult(pack:Pack):void
		{
			var smithyEquipList:Object = pack.smithyEquipList;
			equipRefine.loadOffAll();
			for each(var tempEq:Obj in smithyEquipList)
			{
				var temBox:BoxObj = equipRefine.loadItem(tempEq) as BoxObj;
				if (temBox != null)
				{
					temBox.getAsset().addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
					temBox.getAsset().addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
					
				}
			}
		}
		
		private function onLoadItemOff(e:ItemClickEvent):void
		{
			dispatchLoadOffItemEvent();
		}
		
		private function startDispatchEvent(data:Object):void
		{
			var event:ItemClickEvent = new ItemClickEvent(ItemClickEvent.BLACK_SMITHY_COMMAND);
			event.data = data;
			dispatchEvent(event);	
		}
		
		private  function onMouseOver(e:MouseEvent):void
		{
			var objItem:BasicObjItem = e.currentTarget as BasicObjItem;
			var objTip:Sprite = PackageDialog.getObjSprite(objItem.obj);
			var globalPoint:Point = objItem.parent.localToGlobal(new Point(objItem.x, objItem.y));
			objTip.x = globalPoint.x + objItem.width;
			objTip.y = globalPoint.y + objItem.height;
			var event:ItemClickEvent = new ItemClickEvent(ItemClickEvent.ITEM_MOUSE_OVER);
			event.data = objTip;
			dispatchEvent(event);
		}
		
		private  function onMouseOut(e:MouseEvent):void
		{
			var event:ItemClickEvent = new ItemClickEvent(ItemClickEvent.ITEM_MOUSE_OUT);
			dispatchEvent(event);
		}
		
		private function addTabPane(tabPane:JTabbedPane,leftPane:*, str:String):JTabbedPane
		{
			tabPane.appendTab(leftPane, str);
			return tabPane as JTabbedPane;
		}
		
	}
}
